package com.oig.sys.base.controller;

import com.oig.common.constants.CommonEnum;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.CurrencyDto;
import com.oig.sys.base.service.ITbCurrencyService;
import com.oig.sys.base.vo.CurrencyVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "币种管理")
@RestController
@RequestMapping("/currency")
@Slf4j
public class TbCurrencyController {


    private final ITbCurrencyService currencyService;

    @Autowired
    public TbCurrencyController(ITbCurrencyService currencyService){
        this.currencyService = currencyService;
    }

    @PreAuthorize("@pms.hasPermission('/currency/addCurrency')")
    @SysLog(value="添加币种",apiUrl="/currency/addCurrency")
    @Operation(summary = "添加币种,添加币种")
    @PostMapping(value = "/addCurrency")
    public CurrencyVo addCurrency(@RequestBody @Validated(GroupAdd.class) CurrencyDto currencyDto){
        log.info("add Currency : {}", currencyDto);
        return this.currencyService.addTbCurrency(currencyDto) ;
    }

    @PreAuthorize("@pms.hasPermission('/currency/updateCurrency')")
    @SysLog(value="修改币种",apiUrl="/currency/updateCurrency")
    @Operation(summary = "修改币种,修改币种")
    @PostMapping(value = "/updateCurrency")
    public CurrencyVo updateCurrency(@RequestBody @Validated(GroupEdit.class) CurrencyDto currencyDto){
        log.info("update currency : {}", currencyDto);
        return this.currencyService.updateCurrency(currencyDto) ;
    }

    @PreAuthorize("@pms.hasPermission('/currency/delCurrency')")
    @SysLog(value="物理删除币种",apiUrl="/currency/delCurrency")
    @Operation(summary = "物理删除币种")
    @PostMapping(value = "/delCurrency/{code}")
    public SysResponse<String> delCurrency(@PathVariable("code") String code){
        log.info("del currency code={}", code) ;
        this.currencyService.delCurrency(code);
        return SysResponse.ok(code,"物理删除成功");
    }

    @Operation(summary = "获取币种信息")
    @GetMapping(value = "/getCurrency/{code}")
    public CurrencyVo getCurrency(@PathVariable("code") String code){
        log.info("get currency code={}", code) ;
        return this.currencyService.getCurrency(code);
    }

    @Operation(summary = "查询所有币种，用于选择")
    @GetMapping(value = "/listAll")
    public List<CurrencyVo> listAll(){
        log.info("listAll currency ") ;
        return this.currencyService.listAll(CommonEnum.CommonStatusEnum.OK.getCode());
    }

}
